﻿using System.Net;
using System.Net.NetworkInformation;

namespace ThresholdAssistant.Tools
{
    /// <summary>
    /// 网络信息工具
    /// </summary>
    public class NetworkUtil
    {
        /// <summary>
        /// 获取主机名称
        /// </summary>
        /// <returns></returns>
        public static string GetHostName()
        {
            return Dns.GetHostName();
        }

        /// <summary>
        /// 获取电脑 MAC（物理）地址
        /// 备注：如果主机有多块的时候按照默认取第一块
        /// </summary>
        /// <returns></returns>
        public static string GetMacAddress()
        {
            string macAddress = "";
            var allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();//获取本机所有网络连接
            if (allNetworkInterfaces == null || allNetworkInterfaces.Length < 1)
                return macAddress;

            NetworkInterface networkInterface = allNetworkInterfaces[0];
            PhysicalAddress address = networkInterface.GetPhysicalAddress();
            byte[] bytes = address.GetAddressBytes();
            for (int i = 0; i < bytes.Length; i++)
            {
                macAddress += bytes[i].ToString("X2");
                if (i != bytes.Length - 1)
                {
                    macAddress += "-";
                }
            }
            return macAddress;
        }
    }
}